﻿using System;
using System.IO;
using System.Security.AccessControl;
//using log4net;
//using log4net.Config;

namespace Test
{
	class Program
	{
		//private static readonly ILog logger = LogManager.GetLogger(typeof(Program));

		
		static void Main(string[] args)
		{
			//XmlConfigurator.Configure();

			//logger.Debug("Here is a debug log.");
			//logger.Info("... and an Info log.");
			//logger.Warn("... and a warning.");
			//logger.Error("... and an error.");
			//logger.Fatal("... and a fatal error.");

			DriveInfo[] allDrives = DriveInfo.GetDrives();

			foreach (DriveInfo d in allDrives)
			{
				Console.WriteLine("Drive {0}", d.Name);
				Console.WriteLine("  Drive type: {0}", d.DriveType);
				if (d.IsReady == true)
				{
					Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
					Console.WriteLine("  File system: {0}", d.DriveFormat);
					Console.WriteLine(
						"  Available space to current user:{0, 15} bytes",
						d.AvailableFreeSpace);

					Console.WriteLine(
						"  Total available space:          {0, 15} bytes",
						d.TotalFreeSpace);

					Console.WriteLine(
						"  Total size of drive:            {0, 15} bytes ",
						d.TotalSize);
					Console.WriteLine("Root: ", d.RootDirectory);
				}
			}
			Console.ReadLine();
		}

		public static void Test()
		{
			string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			//string NtAccountName = @"MyDomain\MyUserOrGroup";

			//DirectoryInfo di = new DirectoryInfo(path);
			//try 
			//{ 
			//	DirectorySecurity acl = di.GetAccessControl();
			//	AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));

			//	//Go through the rules returned from the DirectorySecurity
			//	foreach (AuthorizationRule rule in rules)
			//	{
			//		//If we find one that matches the identity we are looking for
			//		if (rule.IdentityReference.Value.Equals(NtAccountName, StringComparison.CurrentCultureIgnoreCase))
			//		{
			//			//Cast to a FileSystemAccessRule to check for access rights
			//			if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData) > 0)
			//			{
			//				Console.WriteLine(string.Format("{0} has write access to {1}", NtAccountName, path));
			//			}
			//			else
			//			{
			//				Console.WriteLine(string.Format("{0} does not have write access to {1}", NtAccountName, path));
			//			}
			//		}
			//	}
			//}
			//catch (System.Security.AccessControl.PrivilegeNotHeldException e) { }
			//catch (System.UnauthorizedAccessException) { }


			DirectoryInfo fInfo = new DirectoryInfo(path);
			DirectorySecurity fsec = fInfo.GetAccessControl();
			AuthorizationRuleCollection aclr = fsec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
			
			foreach (AuthorizationRule ace in aclr)
			{
				Console.WriteLine(ace.IdentityReference.Value);
			}

			DirectoryInfo[] subSubDirs = fInfo.GetDirectories();
			foreach (DirectoryInfo dr in subSubDirs)
			{
				Console.WriteLine(dr.Name);
			}
		}

		public void ExportToFile(string filename)
		{
			//var permissionSet = new PermissionSet(PermissionState.None);
			//var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, filename);
			//permissionSet.AddPermission(writePermission);

			//if (permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
			//{
			//	using (FileStream fstream = new FileStream(filename, FileMode.Create))
			//	using (TextWriter writer = new StreamWriter(fstream))
			//	{
			//		// try catch block for write permissions 
			//		writer.WriteLine("sometext");


			//	}
			//}
			//else
			//{
			//	//perform some recovery action here
			//}

		}
	}
}
